import sys
sys.path.append("..")
from database.db_handler import MysqlHander
from common.my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
from common.entity_data import EntityData
import glob
import jieba
import json
import re
import threading
import hashlib

'''
品牌
'''

class JijinData:
    def __init__(self):
        self.entity = EntityData()
        self.data_map = {}
        pass
    
    def get_data(self):
        for i in range (1,600):
            url = "http://fund.eastmoney.com/data/FundGuideapi.aspx?dt=4&sd=&ed=&sc=3y&st=desc&pi=" + str(i) + "&pn=20&zf=diy&sh=list&rnd=0.49712717055369327"
            json_data = json.loads(MyHttp.http_json_data(url).decode().replace("var rankData =", ""))
            for d in json_data["datas"]:
                ts = d.split(",")
                name = ts[3]
                value = ts[1]
                if name in self.data_map.keys():
                    self.data_map[name] += "|" + value
                else:
                    self.data_map[name] = value
                    
        for k,v in self.data_map.items():
            self.entity.add_entity_data(k + "类型基金", "来源：天天基金网。", 100, 1000005, "", "", v)
            self.entity.update_entity_data(k + "类型基金", "来源：天天基金网。", 100, 1000005, "", "", v)

    def get_data_v2(self):
        url = "https://fund.eastmoney.com/daogou/#dt4;ft;rs;sd;ed;pr;cp;rt;tp;rk;se;nx;sc3y;stdesc;pi1;pn20;zfdiy;shlist"
        bs4_data = MyHttp.bs4_utf8_data(url)
        leixing_map = {}
        for a in bs4_data.find(name="dd", attrs={'id':'dd_tp'}).find_all("a"):
            leixing_map[a.text] = a["id"].replace("tp_", "")

        for a in bs4_data.find(name="dd", attrs={'id':'zzjjafloat'}).find_all("a"):
            leixing_map[a.text] = a["id"].replace("tp_", "")

        for k,v in leixing_map.items():
            set_name = ""
            for i in range(1,600):
                leixing_url = "https://fund.eastmoney.com/data/FundGuideapi.aspx?dt=4&sd=&ed=&tp=" + v + "&sc=3y&st=desc&pi=" + str(i) + "&pn=20&zf=diy&sh=list&rnd=0.6931026402916631"
                json_data = json.loads(MyHttp.http_json_data(leixing_url).decode().replace("var rankData =", ""))
                if len(json_data["datas"]) == 0:
                    break
                for d in json_data["datas"]:
                    ts = d.split(",")
                    set_name += "|" + ts[1]
            self.entity.add_entity_data(k + "主题基金", "来源：天天基金网。", 100, 1000005, "", "", set_name)
            self.entity.update_entity_data(k + "主题基金", "来源：天天基金网。", 100, 1000005, "", "", set_name)

    def get_data_v3(self):
        url = "https://fund.eastmoney.com/daogou/#dt4;ft;rs;sd;ed;pr;cp;rt;tp;rk;se;nx;sc3y;stdesc;pi1;pn20;zfdiy;shlist"
        bs4_data = MyHttp.bs4_utf8_data(url)
        leixing_map = {}
        for a in bs4_data.find(name="ul", attrs={'id':'select_cp'}).find_all("a"):
            leixing_map[a.text] = a["id"].replace("cp_", "").replace("_hot", "")

        for a in bs4_data.find(name="dl", attrs={'id':'content_cp'}).find_all("a"):
            leixing_map[a.text] = a["id"].replace("cp_", "").replace("_hot", "")

        for k,v in leixing_map.items():
            set_name = ""
            for i in range(1,600):
                leixing_url = "https://fund.eastmoney.com/data/FundGuideapi.aspx?dt=4&sd=&ed=&cp=" + v + "&sc=3y&st=desc&pi=" + str(i) + "&pn=20&zf=diy&sh=list&rnd=0.2152137048668301"
                json_data = json.loads(MyHttp.http_json_data(leixing_url).decode().replace("var rankData =", ""))
                if len(json_data["datas"]) == 0:
                    break
                for d in json_data["datas"]:
                    ts = d.split(",")
                    set_name += "|" + ts[1]
            self.entity.add_entity_data(k + "旗下基金", "来源：天天基金网。", 100, 1000005, "", "", set_name)
            self.entity.update_entity_data(k + "旗下基金", "来源：天天基金网。", 100, 1000005, "", "", set_name)

def jijin_api():
    d = JijinData()
    #d.get_data()
    #d.get_data_v2()
    d.get_data_v3()

if __name__ == '__main__':
    jijin_api()
